package v1alpha1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type NginxChaosSpec struct {
	// ContainerSelector specifies the target for injection
	ContainerSelector `json:",inline"`

	// Duration represents the duration of the chaos
	// +optional
	Duration *string `json:"duration,omitempty"`

	// Action 取值范围config, crash, response, restart
	Action NginxAction `json:"action"`

	// ConfigMode 修改配置的模式，取值范围file、cmd
	// +optional
	ConfigMode string `json:"mode"`
	// ConfigFile 修改后的配置文件内容
	// +optional
	ConfigFile string `json:"file"`
	// ConfigBlock 修改的配置块，比如http.server[0].location[0]
	// +optional
	ConfigBlock string `json:"block"`
	// ConfigSet 一个或多个键值对的形式，表示设置的配置项和值，如proxy_pass=www.baidu.com
	// +optional
	ConfigSet []string `json:"setConfig"`
	// ConfigServerID nginx.conf可能存在多个server，选择目标序号
	// +optional
	ConfigServerID int `json:"serverID"`
	// PathType 请求路径格式，literal或regex表示字面值或正则表达式
	// +optional
	PathType string `json:"pathType"`
	// Path 请求路径内容
	// +optional
	Path string `json:"path"`
	// Code 响应状态码
	// +optional
	Code int `json:"code"`
	// ContentType 响应内容类型，json或txt
	// +optional
	ContentType string `json:"contentType"`
	// Header 响应头
	// +optional
	Header []string `json:"header"`
	// Body 响应体
	// +optional
	Body string `json:"body"`

	// RemoteCluster represents the remote cluster where the chaos will be deployed
	// +optional
	RemoteCluster string `json:"remoteCluster,omitempty"`
}

type NginxAction string

// config, crash, response, restart
const (
	NginxActionConfig   NginxAction = "config"
	NginxActionCrash    NginxAction = "crash"
	NginxActionResponse NginxAction = "response"
	NginxActionRestart  NginxAction = "restart"
)

// +kubebuilder:object:root=true
// +kubebuilder:printcolumn:name="action",type=string,JSONPath=`.spec.action`
// +kubebuilder:printcolumn:name="duration",type=string,JSONPath=`.spec.duration`
// +chaos-mesh:experiment

type NginxChaos struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   NginxChaosSpec             `json:"spec"`
	Status BladeOnlyCreateChaosStatus `json:"status,omitempty"`
}

var _ InnerObjectWithCustomStatus = (*NginxChaos)(nil)
var _ InnerObjectWithSelector = (*NginxChaos)(nil)
var _ InnerObject = (*NginxChaos)(nil)

func init() {
	SchemeBuilder.Register(&NginxChaos{}, &NginxChaosList{})
}

// GetSelectorSpecs is a getter for selectors
func (obj *NginxChaos) GetSelectorSpecs() map[string]interface{} {
	return map[string]interface{}{
		".": &obj.Spec.ContainerSelector,
	}
}

func (obj *NginxChaos) GetCustomStatus() interface{} {
	return &obj.Status.BladeOnlyCreateChaosCustomStatus
}
